package top.chengdongqing.mishop.user.address

import org.springframework.http.HttpStatus
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/users/addresses")
class AddressController(private val addressService: AddressService) {

    @GetMapping
    fun findAddresses(@AuthenticationPrincipal userId: Int): List<ShippingAddressDTO> {
        return addressService.findAddresses(userId)
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    fun addAddress(@AuthenticationPrincipal userId: Int, @RequestBody addressDTO: ShippingAddressDTO) {
        addressService.addAddress(userId, addressDTO)
    }

    @PutMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    fun updateAddress(
        @AuthenticationPrincipal userId: Int,
        @PathVariable id: Int,
        @RequestBody addressDTO: ShippingAddressDTO,
    ) {
        addressService.updateAddress(userId, id, addressDTO)
    }

    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    fun deleteAddress(@AuthenticationPrincipal userId: Int, @PathVariable id: Int) {
        addressService.deleteAddress(userId, id)
    }
}